﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Navigant.Infrastructure.Helpers;
using Navigant.Model.Cases;
using Navigant.Model.Custodians;
using Navigant.Model.PreservationNotices;
using System.Collections.Generic;

namespace Navigant.Web.UI
{
    public class PreservationNoticePreview : Page
    {
        protected PlaceHolder preview;
        protected Label errorMessage;
        private int templateId;
        private bool isReminder;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.templateId = 0;
            this.errorMessage.Visible = false;
            if (!string.IsNullOrEmpty(this.Request["templateId"]))
            {
                int result;
                bool parsed = int.TryParse(this.Request["templateId"], out result);
                if (parsed)
                {
                    this.templateId = result;
                }
 
            }
            if (this.templateId < 1)
            {
                this.errorMessage.Text = "Missing Preservation Notice template.";
                this.errorMessage.Visible = true;
            }
            this.isReminder = false;
            if (!string.IsNullOrEmpty(this.Request["isReminder"]))
            {
                bool result;
                bool parsed = bool.TryParse(this.Request["isReminder"], out result);
                if (parsed)
                {
                    this.isReminder = result;
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            Case currentCase = null;
            try
            {
                using (SPWeb web = SPControl.GetContextSite(this.Context).OpenWeb())
                {
                    currentCase = CaseService.GetCaseBy(new Uri(web.Url));
                    PreservationNoticeTemplateService service = new PreservationNoticeTemplateService(currentCase);
                    PreservationNoticeTemplate template = service.FindBy(this.templateId);
                    if (template == null)
                    {
                        this.errorMessage.Text = "Could not find the Preservation Notice template specified.";
                        this.errorMessage.Visible = true;
                        return;
                    }
                    CustodianService custodianService = new CustodianService(currentCase);
                    IList<Custodian> custodians = custodianService.FindAll();
                    if (custodians == null || custodians.Count == 0)
                    {
                        this.errorMessage.Text = "You must have at least one Custodian defined for the case.";
                        this.errorMessage.Visible = true;
                        return;
                    }
                    Custodian custodian = custodians[0];
                    PreservationNotice notice = template.Merge(custodian,
                        this.isReminder, "test@donotreply.com", true);
                    LiteralControl noticeBody = new LiteralControl(notice.Body);
                    this.preview.Controls.Add(noticeBody);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, currentCase);
                this.errorMessage.Text = "There was an error previewing the Preservation Notice.";
                this.errorMessage.Visible = true;
            }
        }
    }
}
